<script setup lang="ts">
import { FileUpload } from "@/components/common/FileUpload";
import { numberToChinese } from "@/utils/common";
import { clearNoNum, clearSuffix } from "@/utils/inupt";
import Delete from "@iconify-icons/ep/delete";
import AddFill from "@iconify-icons/ep/plus";
import { ref } from "vue";
import { formRules } from "../utils/rule";
import { FormProps } from "../utils/types";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: null,
    title: "新增",
    verticalProjectName: "",
    startDate: "",
    endDate: "",
    projectDate: [],
    amountList: [],
    level: "",
    contractFiles: "",
    contractSignAddress: "",
    contractSignTime: "",
    finishFiles: "",
    projectType: 2,
    accountPrice: null,
    firstAccountPrice: null,
    firstAccountDate: null,
    secondAccountPrice: null,
    secondAccountDate: null,
    contractPrice: null,
    score: 0
  }),
  userList: null
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    :inline="true"
    label-width="110px"
    class="add-form"
  >
    <el-form-item label="横向项目名称" prop="verticalProjectName">
      <el-input
        v-model="newFormInline.verticalProjectName"
        placeholder="请输入"
      />
    </el-form-item>
    <el-form-item label="项目负责人" prop="projectMasterName">
      <el-input
        v-model="newFormInline.projectMasterName"
        placeholder="请输入"
      />
    </el-form-item>
    <el-form-item label="甲方单位" prop="level">
      <el-input
        v-model="newFormInline.level"
        placeholder="请输入"
        class="!w-[300px]"
      />
    </el-form-item>
    <el-form-item label="合同金额" prop="contractPrice">
      <el-input
        v-model="newFormInline.contractPrice"
        placeholder="请输入"
        @input="$event => (newFormInline.contractPrice = clearNoNum($event))"
        @blur="
          newFormInline.contractPrice = clearSuffix(newFormInline.contractPrice)
        "
      >
        <template #suffix> 万元 </template>
      </el-input>
    </el-form-item>

    <div v-for="(item, index) in newFormInline.amountList" :key="index">
      <el-form-item
        :label="`第${numberToChinese(index + 1)}笔到账金额`"
        prop="firstAccountPrice"
      >
        <el-input
          v-model="item.accountPrice"
          placeholder="请输入"
          @input="$event => (item.accountPrice = clearNoNum($event))"
          @blur="item.accountPrice = clearSuffix(item.accountPrice)"
        >
          <template #suffix> 万元 </template>
        </el-input>
      </el-form-item>
      <el-form-item
        :label="`第${numberToChinese(index + 1)}笔到账时间`"
        prop="firstAccountDate"
      >
        <div class="flex items-center">
          <el-date-picker
            v-model="item.accountDate"
            type="date"
            placeholder="请选择"
            value-format="YYYY-MM-DD"
            class="flex-1 mr-[10px]"
          />
          <IconifyIconOffline
            v-if="index != 0"
            class="text-red-500 text-lg cursor-pointer mr-[8px]"
            :icon="Delete"
            @click="newFormInline.amountList.splice(index, 1)"
          />
          <IconifyIconOffline
            class="cursor-pointer text-lg"
            :icon="AddFill"
            @click="
              newFormInline.amountList.push({
                accountPrice: '',
                accountDate: ''
              })
            "
          />
        </div>
      </el-form-item>
    </div>

    <el-form-item label="项目日期" prop="startDate">
      <el-date-picker
        v-model="newFormInline.projectDate"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="YYYY-MM-DD"
      />
    </el-form-item>

    <el-form-item label="项目签订日期" prop="contractSignTime">
      <el-date-picker
        v-model="newFormInline.contractSignTime"
        type="date"
        placeholder="请选择"
        value-format="YYYY-MM-DD"
        class="!w-full"
      />
    </el-form-item>

    <el-form-item label="合同签订地点" prop="contractSignAddress">
      <el-input
        v-model="newFormInline.contractSignAddress"
        placeholder="请输入"
      />
    </el-form-item>
    <el-form-item />
    <el-form-item label="结题报告附件" prop="finishFiles">
      <FileUpload v-model="newFormInline.finishFiles" :limit="15" />
    </el-form-item>

    <el-form-item label="合同附件" prop="contractFiles">
      <FileUpload v-model="newFormInline.contractFiles" :limit="15" />
    </el-form-item>

    <el-form-item label="获取积分" prop="score">
      <el-input-number
        v-model="newFormInline.score"
        :min="0"
        placeholder="请输入"
        class="!w-[200px]"
      />
    </el-form-item>
    <el-form-item />
  </el-form>
</template>
<style lang="scss" scoped>
.add-form {
  text-align: center;
  .el-form-item {
    width: 44%;
  }
}
</style>
